<?php

namespace app\admin\model;

/**
 * 菜单模型
 * Class Menu
 * @package app\admin\model
 */
class Menu extends Base
{

    //链接类型获取器
    public function getTypeAttr($value)
    {
        $MenuTypeRadio = config('MenuTypeRadio');
        return $MenuTypeRadio[$value];
    }

    //全部菜单列表
    public function menuList($keyword = ""){
        return $this
            ->field([
                "menu_id",
                "menu_name",
                "menu_url",
                "is_left",
                "sort",
                "type",
                "is_authority",
                "parent_id"
            ])
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->where("menu_name","like","%".$keyword."%");
                }
            })
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->order("sort DESC")
            ->select();
    }

    //获取用户菜单
    public function userMenuList(){
        $result = $this
            ->field([
                "menu_id",
                "menu_name",
                "menu_url",
                "type",
            ])
            ->where("is_left",1)//获取显示的
            ->where("parent_id",0)//获取顶级的
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->where(function ($q){
                if($this->admin["group_id"] != config("SuperAdminGroupId")) {//不是超级管理员获取自己菜单
                    $AdminGroup = new AdminGroup();
                    $menus = $AdminGroup->getFieldValue("group_id", $this->admin["group_id"], "menus");
                    if(!empty($menus)){//没有任何权限
                        $menus = StrToArray($menus);//获取数组
                        $q->where("menu_id","IN",$menus);
                    }else{
                        $q->where("menu_id",0);
                    }
                }
            })
            ->order("sort DESC")
            ->select();
        foreach ($result as $key=>$val){
            $result[$key]["subset"] = $this->getSubsetList($val["menu_id"]);
        }
        return $result;
    }

    //获取下级菜单
    public function getSubsetList($parent_id){
        return $this
            ->field([
                "menu_id",
                "menu_name",
                "menu_url",
                "type",
            ])
            ->where("is_left",1)//获取显示的
            ->where("parent_id",$parent_id)//获取顶级的
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->where(function ($q){
                if($this->admin["group_id"] != config("SuperAdminGroupId")) {//不是超级管理员获取自己菜单
                    $AdminGroup = new AdminGroup();
                    $menus = $AdminGroup->getFieldValue("group_id", $this->admin["group_id"], "menus");
                    if(!empty($menus)){//没有任何权限
                        $menus = StrToArray($menus);//获取数组
                        $q->where("menu_id","IN",$menus);
                    }else{
                        $q->where("menu_id",0);
                    }
                }
            })
            ->order("sort DESC")
            ->select();
    }

    //验证URL是否存在
    public function verificationUrl(){
        $url = strtolower(request()->module() . "/" . request()->controller() . "/" . request()->action());
        $menu_id = $this->where("LOWER(menu_url) =:menu_url")->bind(['menu_url'=>$url])->value("menu_id");
        if(empty($menu_id)){
            $msg = "访问地址不存在";
            if(request()->isAjax()) {
                echo json_encode(["msg" => $msg], JSON_UNESCAPED_UNICODE);
            }else{
                echo $msg;
            }
            exit;
        }
        return $menu_id;
    }

    //权限设置菜单列表
    public function setMenuList($group_id){
        return $this
            ->field([
                "menu_id",
                "menu_name",
                "menu_url",
                "is_left",
                "sort",
                "type",
                "is_authority",
                "parent_id"
            ])
            ->where("is_authority",1)//获取需要验证权限的
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->where(function ($q)use ($group_id){
                //权限判断子角色只能继承父角色权限
                $AdminGroup = new AdminGroup();
                $group_pid_id = $AdminGroup->getFieldValue("group_id", $group_id, "parent_id");//获取父级ID
                if($group_pid_id != config("SuperAdminGroupId")) {//父级不是超级管理员只获取上级拥有的权限
                    $menus = $AdminGroup->getFieldValue("group_id", $group_pid_id, "menus");//获取父级权限
                    if (!empty($menus)) {//没有任何权限
                        $menus = StrToArray($menus);//获取数组
                        $q->where("menu_id", "IN", $menus);
                    } else {
                        $q->where("menu_id", 0);
                    }
                }
            })
            ->order("sort DESC")
            ->select();
    }

    //验证权限设置菜单列表
    public function checkSetMenuList($group_id,$menus){
        $count = 0;
        $setMenuList = $this->setMenuList($group_id);
        if(!empty($menus)) {
            foreach ($menus as $val) {
                foreach ($setMenuList as $m_val) {
                    if ($val == $m_val["menu_id"]) {
                        $count++;
                        break;
                    }
                }
            }
        }
        return $count == count($menus) ? true :false;
    }
}